package com.example.imoocmall.controller;

import com.example.imoocmall.common.ApiRestResponse;
import com.example.imoocmall.common.Constant;
import com.example.imoocmall.exception.ImoocMallException;
import com.example.imoocmall.exception.ImoocMallExceptionEnum;
import com.example.imoocmall.model.pojo.User;
import com.example.imoocmall.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制器
 */
@RestController
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    UserServiceImpl userServiceimpl;

    /**
     * 测试
     * @return
     */
    @ApiOperation("测试项目启动并连接数据库")
    @GetMapping("/test")
    public User personalPage(){

        return userServiceimpl.getUser();
    }
    //注册

    @ApiOperation("注册")
    @PostMapping("/register")
    public ApiRestResponse register(@RequestParam("userName")String userName, @RequestParam("password") String password) throws ImoocMallException {
        if(userName==null || "".equals(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if(password==null || "".equals(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        //对密码长度的校验
        if (password.length()<8){
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        userServiceimpl.register(userName,password);
        return ApiRestResponse.success();

    }
    //登录
    @ApiOperation("登录")
    @PostMapping("/login")
    public ApiRestResponse login(@RequestParam("userName")String userName, @RequestParam("password") String password, HttpServletRequest request) throws ImoocMallException {
        if(userName==null || "".equals(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if(password==null || "".equals(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userServiceimpl.login(userName,password);
        //保存用户信息时,不保存密码
        user.setPassword("*********");
        request.getSession().setAttribute(Constant.IMOOC_MALL_USER,user);//传入session
        return ApiRestResponse.success(user);
    }

    //更新个性签名
    @ApiOperation("更新个性签名")
    @PostMapping("/user/update")
    public ApiRestResponse updatauserinfo(HttpServletRequest request,@RequestParam String signature) throws ImoocMallException {
        User currentUser = (User)request.getSession().getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userServiceimpl.updateInfomation(user);
        return ApiRestResponse.success();
    }

    //退出登录
    @ApiOperation("退出登录")
    @PostMapping("/user/logout")
    public ApiRestResponse logout(HttpServletRequest request){
        request.getSession().removeAttribute(Constant.IMOOC_MALL_USER);
        return ApiRestResponse.success();
    }

    //管理员用户登录
    @ApiOperation("管理员用户登录")
    @PostMapping("/adminlogin")
    public ApiRestResponse adminlogin(@RequestParam("userName")String userName, @RequestParam("password") String password, HttpServletRequest request) throws ImoocMallException {
        if(userName==null || "".equals(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if(password==null || "".equals(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userServiceimpl.login(userName,password);
        //校验是否是管理员
        if (userServiceimpl.checkAdminRole(user)) {
            //保存用户信息时,不保存密码
            user.setPassword("********");
            request.getSession().setAttribute(Constant.IMOOC_MALL_USER,user);
            return ApiRestResponse.success(user);
        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }

}
